<template>
  <div class="app-container">
    <el-card :bordered="false">
      <el-form  ref="formValidate" :rules="ruleValidate" :model="formValidate" label-width="130px" >
        <!-- <el-row :gutter="24"> -->
          <!-- 商品信息-->
          <div class="formBody">
           <div >
              <el-form-item label="地区：" prop="area">
                 <el-cascader ref="refSubCat"  v-model="formValidate.area"  style="width: 100%"   @change='changeHandle'
                 :props="props"   :key="flag"   :disabled="identNumber == 2||identNumber == 3"></el-cascader>
            </el-form-item>
          </div>
         
         
           <div >
              <el-form-item label="所属用户" prop="userId">
                 <!-- <el-select v-model="formValidate.userId"   :disabled="identNumber == 2||identNumber == 3"  clearable placeholder="请选择所属用户" class="filter-item" style="width: 100%" @change="SelectLabel">
                   <el-option v-for="item in userListOptions" :key="item.uid" :label="item.username" :value="item.uid" />
                  </el-select> -->
                      <el-autocomplete
                            clearable
                            class="inline-input"
                            v-model="formValidate.userName"
                           :fetch-suggestions="querySearch"
                           placeholder="请输入所属用户"
                           @select="handleSelect"
                           value-key="username"
                      ></el-autocomplete>
                </el-form-item>
          </div>
          
          <div >
            <el-form-item label="手机号：" prop="mobile">
              <el-input v-model="formValidate.mobile" placeholder="请输入手机号"   :disabled="identNumber == 2||identNumber == 3"/>
            </el-form-item>
          </div>
            <div class="radioAlign">
             <el-form-item label="审核结果："  prop="" v-if="identNumber != 1">
              <el-radio-group v-model="formValidate.status"  @change="changeSpec"  :disabled="identNumber == 3">
                <el-radio :label="1" class="radio">通过</el-radio>
                <el-radio :label="2">不通过</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
            <div >
              <el-form-item label="不通过理由：" prop="explain" v-if="formValidate.status==2">
              <el-input   v-model="formValidate.explain" placeholder="请输入不通过理由" type="textarea" :rows="3"  :disabled="identNumber == 3"/>
            </el-form-item>
          </div>
         <el-form-item>
          <el-button  v-if="identNumber !=3" type="primary" class="submission" @click="handleSubmit('formValidate')">提交</el-button>
          <el-button  v-if="identNumber !=3" type="" class="submission" @click="handleCancel('formValidate')">取消</el-button>
           <el-button type="primary" class="submission" @click="handleBack('formValidate')" v-if="identNumber ==3">返回</el-button>
        </el-form-item>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  add,
  userList,
  update,
  getInfo,
  audit,
  isFormatAttr,
  getArea,
} from "@/api/nodeManage";
import editor from "../../components/Editor";
export default {
  components: {
    editor,
  },
  data() {
    return {
      spinShow: false,
      queryTypeOptions: [
        { key: "zs", display_name: "张三" },
        { key: "ls", display_name: "李四" },
      ],
      userListOptions: [],
      identNumber: "",
      grid2: {
        xl: 10,
        lg: 12,
        md: 12,
        sm: 24,
        xs: 24,
      },
      grid3: {
        xl: 18,
        lg: 18,
        md: 20,
        sm: 24,
        xs: 24,
      },
      watchIndex: 0,
      flag: false,
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          if (level == 0) {
            getArea().then(res => {
              let nodes = res.map((item, index) => {
                return {
                  value: item.code,
                  label: item.name,
                  leaf: false,
                };
              });

              resolve(nodes);
            });
          } else if (level == 1) {
            getArea({ code: node.data.value }).then(res => {
              let nodes = res.map((item, index) => {
                return {
                  value: item.code,
                  label: item.name,
                  leaf: false,
                };
              });
              resolve(nodes);
            });
          } else if (level == 2) {
            getArea({ code: node.data.value }).then(res => {
              let nodes = res.map((item, index) => {
                return {
                  value: item.code,
                  label: item.name,
                  leaf: true,
                };
              });
              resolve(nodes);
            });
          }
        },
      },
      // 批量设置表格data
      oneFormBatch: [
        {
          pic: "",
          price: 0,
          cost: 0,
          ot_price: 0,
          stock: 0,
          bar_code: "",
          seckill_stock: 0,
          seckill_price: 0,
          pink_stock: 0,
          pink_price: 0,
          weight: 0,
          volume: 0,
        },
      ],
      // 规格数据
      formDynamic: {
        attrsName: "",
        attrsVal: "",
      },
      formDynamicNameData: [],
      isBtn: false,
      myConfig: {
        autoHeightEnabled: false, // 编辑器不自动被内容撑高
        initialFrameHeight: 500, // 初始容器高度
        initialFrameWidth: "100%", // 初始容器宽度
        UEDITOR_HOME_URL: "/UEditor/",
        serverUrl: "",
      },
      columns: [],
      formValidate: {
        mobile: "",
        userId: "",
        userName: "",
        area: [],
        status: 1,
        provinceCode: "",
        cityCode: "",
        areaCode: "",
        provinceName: "",
        cityName: "",
        areaName: "",
        explain: "",
      },
      ruleList: [],
      templateList: [],
      createBnt: false,
      showIput: false,
      manyFormValidate: [],
      // 单规格表格data
      oneFormValidate: [
        {
          imageArr: [],
          pic: "",
          price: 2,
          cost: 0,
          ot_price: 0,
          stock: 0,
          seckill_stock: 0,
          seckill_price: 0,
          pink_stock: 0,
          pink_price: 0,
          bar_code: "",
          weight: 0,
          volume: 0,
          brokerage: 0,
          brokerage_two: 0,
        },
      ],
      images: [],
      grid: {
        xl: 8,
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      optionsMetaShow: [],
      tableIndex: 0,
      ruleValidate: {
        store_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        store_info: [
          { required: true, message: "请输入商品简介", trigger: "blur" },
        ],
      },
      attrs: [],
    };
  },
  watch: {
    "formValidate.area": {
      deep: true,
      handler(newValue, oldValue) {
        console.log(newValue, oldValue, "111");
        this.watchIndex++;
        if (this.watchIndex === 1) {
          this.flag = !this.flag;
          console.log(this.flag);
        }
      },
    },
  },
  mounted() {
    this.getInfo();
    this.userList();
  },
  methods: {
    // 详情
    getInfo() {
      let that = this;
      let id = that.$route.params.id || 0;
      that.identNumber = that.$route.query.identNumber;
      console.log(id);
      getInfo(id).then(async res => {
        let data = res;
        if (data) {
          that.formValidate = data;
          that.$set(that.formValidate, "area", [
            data.provinceCode,
            data.cityCode,
            data.areaCode,
          ]);
        }
        if (that.identNumber == 2) {
          console.log(that.identNumber, "2222");
          this.formValidate.status = 1;
        } else {
          that.formValidate.status = Number(data.status);
          this.formValidate.explain = data.auditExplain;
        }
      });
    },
    querySearch(queryString, cb) {
      userList().then(res => {
        this.userListOptions = res.data;

        var results = queryString
          ? this.userListOptions.filter(this.createFilter(queryString))
          : this.userListOptions;
        // 调用 callback 返回建议列表的数据
        cb(results);
      });
    },
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.username
            .toLowerCase()
            .indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect(item) {
      this.formValidate.userId = item.uid;
      console.log(item);
    },
    // 改变审核结果
    changeSpec() {},
    SelectLabel(val) {
      this.formValidate.userName = val
        ? this.userListOptions.find(ele => ele.uid === val).username
        : "";
    },
    //所属用户列表
    userList() {
      // userList().then(res => {
      //   this.userListOptions = res.data;
      // });
    },
    // 提交
    handleSubmit(name) {
      let id = this.$route.params.id || 0;
      this.$refs[name].validate(valid => {
        if (valid) {
          //添加
          if (this.identNumber == 1) {
            this.formValidate.provinceCode = this.formValidate.area[0];
            this.formValidate.cityCode = this.formValidate.area[1];
            this.formValidate.areaCode = this.formValidate.area[2];
            add(this.formValidate)
              .then(async res => {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$router.push({ path: "/nodeManage/nodeList" });
                }, 500);
              })
              .catch(res => {
                // this.$message({
                //   message:res.message,
                //   type: 'error'
                // });
              }); //审核
          } else if (this.identNumber == 2) {
            audit({
              nodeId: id,
              status: this.formValidate.status,
              explain: this.formValidate.explain,
            })
              .then(async res => {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$router.push({ path: "/nodeManage/nodeList" });
                }, 500);
              })
              .catch(res => {
                // this.$message({
                //   message:res.message,
                //   type: 'error'
                // });
              });
          } else if (this.identNumber == 4) {
            //修改
            this.formValidate.provinceCode = this.formValidate.area[0];
            this.formValidate.cityCode = this.formValidate.area[1];
            this.formValidate.areaCode = this.formValidate.area[2];
            update(this.formValidate)
              .then(async res => {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$router.push({ path: "/nodeManage/nodeList" });
                }, 500);
              })
              .catch(res => {
                // this.$message({
                //   message:res.message,
                //   type: 'error'
                // });
              });
          }
        } else {
        }
      });
    },

    // 取消-清空表单
    handleCancel(name) {
      if (this.identNumber == 1 || this.identNumber == 4) {
        (this.formValidate.mobile = ""), (this.formValidate.userName = "");
        this.formValidate.area = [];
        this.formValidate.userId = "";
        // this.formValidate.status = "";
        this.formValidate.explain = "";
      }
    },
    // 表单验证
    validate(prop, status, error) {
      if (status === false) {
        this.$message.warning(error);
      }
    },
    //选择区域
    changeHandle(event) {
      this.formValidate.provinceName = this.$refs[
        "refSubCat"
      ].getCheckedNodes()[0].parent.parent.label;
      this.formValidate.cityName = this.$refs[
        "refSubCat"
      ].getCheckedNodes()[0].parent.label;
      this.formValidate.areaName = this.$refs[
        "refSubCat"
      ].getCheckedNodes()[0].label;
    },
    //详情返回
    handleBack() {
      this.$router.push({ path: "/nodeManage/nodeList" });
    },
  },
};
</script>

<style scoped lang="stylus">
.el-autocomplete {
  width: 100%;
}

.formBody {
  width: 50%;
  display: inline-block;
}

/deep/.radioAlign .el-form-item__content {
  text-align: left;
}

.el-form {
  text-align: center;
}

.el-form-item--small.el-form-item {
  margin-bottom: 20px;
}

.submission {
  margin-left: 10px;
}

.color-list .tip {
  color: #c9c9c9;
}

.color-list .color-item {
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  color: #fff;
  margin-right: 10px;
}

.color-list .color-item.blue {
  background-color: #1E9FFF;
}

.color-list .color-item.yellow {
  background-color: rgb(254, 185, 0);
}

.color-list .color-item.green {
  background-color: #009688;
}

.columnsBox {
  margin-right: 10px;
}

.priceBox {
  width: 100%;
}

.rulesBox {
  display: flex;
  flex-wrap: wrap;
}

.curs {
  cursor: pointer;
}
</style>
